<?php $this->load->view('scenic/layout/header') ?>
<header class="map-head sec-head flex">
    <div class="sec-return">
        <a href="javascript:goHistoryBack('<?php echo site_url('scenic/info/index/'.$sceneryBase->sid);?>');">
            <i class="iconfont">&#xe606;</i>
        </a>
    </div>
    <div class="sec-city">
        <?php echo $sceneryBase->name_hanzi;?>
    </div>
</header>
<section class="map-tion" id="bzMap"
         data-point='{
            "x":"<?php echo $sceneryBase->baidulng ?>",
            "y":"<?php echo $sceneryBase->baidulat ?>",
            "title":"<?php echo $sceneryBase->name_hanzi ?>",
            "city":"<?php echo mb_strstr(trim(mb_strstr($sceneryBase->address, ' ')), ' ', true); ?>",
            "address":"<?php echo $sceneryBase->address ?>"
         }'>
</section>
<footer class="map-foot">
    <a href="javascript:;" id="map-gps">
        <i class="iconfont">&#xe602;</i>
        <p>位置</p>
    </a>
    <a href="javascript:;" id="map-scenic">
        <i class="iconfont">&#xe613;</i>
        <p>景点</p>
    </a>
</footer>
<div class="gps-state">
    定位中，请稍后...
</div>
<?php $this->load->view('scenic/layout/footer') ?>
<!--  百度地图  -->
<script type="text/javascript">
    function mapScript() {
        var script = document.createElement('script');
        script.type = "text/javascript";
        script.src = "https://api.map.baidu.com/api?v=2.0&ak=4E7D433Eb897fba7c7a92532c288b518&callback=init&s=1";
        document.body.appendChild(script);
    }
    
    function init()
    {   
        var map = new BMap.Map('bzMap', {minZoom:9,maxZoom:18,enableMapClick:false});
        var points =$('#bzMap').data('point');
        if (points.x!=0 && points.y!=0) {
            var point = new BMap.Point(points.x, points.y);
            map.centerAndZoom(point, 14);
            map.clearOverlays();        //清除所有覆盖物
            //图标
            var myIcon1 = new BMap.Icon("/wap/scenic/images/dingwei.png", new BMap.Size(33, 33));
            var marker1 = new BMap.Marker(point, {icon:myIcon1});
            map.addOverlay(marker1);     
            // 信息
           var sContent = 
                   "<h4 class='ress'>"+points.title+"</h4>"+
                   "<p class='rext'>地址："+points.address+"</p>"+
               "</div>";
            var opts = {
                width : 200         // 信息窗口宽度
            }
            var infoWindow = new BMap.InfoWindow(sContent,opts); 
            map.openInfoWindow(infoWindow,point);
            marker1.addEventListener("click", function(){ 
                map.openInfoWindow(infoWindow,point);
            });
       } else {
           map.centerAndZoom(points.city, 12);
           var options = {
              renderOptions:{map: map},
              pageCapacity:1
           }
           if (points.city=='') {
              points.city = map;
           }
           var local = new BMap.LocalSearch(points.city, options);
           local.search(points.title);       /* 景区名称 */
       }

        $('.map-foot').on('click', '#map-gps', function(event){
            $('.gps-state').addClass('active');
            var geolocation = new jQuery.AMUI.Geolocation({
                enableHighAccuracy: true,
                timeout: 5000,
                maximumAge: 60000
            });
            geolocation.get().then(function(position){
                var coords = {
                    x:position.coords.longitude,
                    y:position.coords.latitude
                };
                var ggPoint = new BMap.Point(coords.x,coords.y); //定位点
                setTimeout(function(){
                    var convertor = new BMap.Convertor();       //谷歌转百度坐标
                    var pointArr = [];
                    pointArr.push(ggPoint);
                    convertor.translate(pointArr, 3, 5, translateCallback);
                }, 1000);

                translateCallback = function (data){        //回调函数
                  if (data.status === 0) {
                        map.clearOverlays();        //清除所有覆盖物
                        var myIcon = new BMap.Icon("/wap/scenic/images/ditudian.gif", new BMap.Size(33, 33));
                        var marker2 = new BMap.Marker(data.points[0], {icon:myIcon});
                        map.addOverlay(marker2);            //放置图标
                        map.setZoom(14); //缩放
                        map.setCenter(data.points[0]);      //设置中心点
                        $('.gps-state').removeClass('active');
                    }
                }
            }, function(error) {
                console.log(error);
            });

            event.stopPropagation();
        });
    }
    
    $('.map-foot').on('click','#map-scenic',function(event){
        init();
        event.stopPropagation();
    });

    window.onload = mapScript;  //异步加载地图
</script>